package com.diy.sigmund.diycommon;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.diy.sigmund.diycommon.middleware.apollo.ApolloProfileConfigUtil;
import com.diy.sigmund.diycommon.spring.config.CustomHttpClientPoolConfig;
import com.diy.sigmund.diycommon.spring.config.dynamicthreadpool.DynamicThreadPoolProperties;
import com.diy.sigmund.diycommon.spring.extensionpoint.CustomApplicationContextInitializer;
import com.diy.sigmund.diycommon.test.spi.HelloSPI;
import java.util.ServiceLoader;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

// exposeProxy设置为true表示暴露代理对象,可以通过AopContext.currentProxy()获取当前对象的代理对象
@EnableAspectJAutoProxy(proxyTargetClass = false, exposeProxy = true)
@EnableApolloConfig
// SpringBoot 如何让yml,properties配置文件有提示:https://blog.csdn.net/xiongmaojiuxianfly/article/details/86630219
// @EnableConfigurationProperties注解表示对@ConfigurationProperties的内嵌支持，默认会将对应Properties Class作为bean注入的IOC容器中，
// 即在相应的Properties类上不用加@Component注解。
@EnableConfigurationProperties({CustomHttpClientPoolConfig.class, DynamicThreadPoolProperties.class})

// 模式1:可以支持其他module启动
// 排除ControllerLogAspect，避免依赖该工程的其它工程打印控制层日志/actuator/health
// @ComponentScan(excludeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ControllerLogAspect.class),})
// @SpringBootConfiguration
// @EnableAutoConfiguration

// 模式2:diy-common包自测
@MapperScan(basePackages = "com.diy.sigmund.diycommon.mapper")
@SpringBootApplication
public class DiyCommonApplication {

    private static final Logger log = LoggerFactory.getLogger(DiyCommonApplication.class);

    public static void main(String[] args) {
        // SPI机制
        ServiceLoader<HelloSPI> serviceLoader = ServiceLoader.load(HelloSPI.class);
        for (HelloSPI helloSPI : serviceLoader) {
            helloSPI.sayHello();
        }


        ApolloProfileConfigUtil.setProfilesActive();
        SpringApplication springApplication = new SpringApplication(DiyCommonApplication.class);
        springApplication.addInitializers(new CustomApplicationContextInitializer());
        springApplication.run(args);

        // 以下为测试代码
        Config applicationConfig = ConfigService.getAppConfig();
        String someKey = "timeout";
        String someDefaultValue = "someDefaultValueForTheKey";
        String value = applicationConfig.getProperty(someKey, someDefaultValue);
        log.info("Apollo配置 timeout={}", value);
        String value1 = applicationConfig.getProperty("test1", someDefaultValue);
        log.info("Apollo配置 test1={}", value1);
    }

}
